 class Left {
     static of (value) {
        return new Left(value)
     }

     constructor (value) {
         this._value = value
     }
     
     map (fn) {
        return this
     }
 }

 class Right {
     static of (value) {
         return new Right(value)
     }

     constructor (value) {
         this._value = value
     }

     map (fn) {
         return Right.of(fn(this._value))
     }
 }

//  const r1 = Right.of(12).map(x => x + 2)
//  const r2 = Left.of(12).map(x => x + 2)

//  console.log(r1, r2)

function parseJSON (str) {
    try {
        return Right.of(JSON.parse(str))
    } catch (error) {
        return Left.of( {error: error.message })
    }
}
// const r = parseJSON('{ name: zs }')
const r = parseJSON('{ "name": "zs" }').map( x => x.name.toUpperCase())
console.log(r)